package com.bosssoft.hr.train.jsp.example.rommapper;

import com.bosssoft.hr.train.j2se.example.database.RowMapper;
import com.bosssoft.hr.train.jsp.example.pojo.User;
import lombok.extern.slf4j.Slf4j;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @description 具体的User映射类 描述rs -》 userList
 * @author ChenTong
 * @date 2020/5/29
 */
@Slf4j
public class UserRowMapper implements RowMapper<List<User>> {

    /**
     * 将数据库读出来result set 映射成 javabean对象
     * @param resultSet
     * @return List
     * @throws Exception
     */
    @Override
    public List<User> mapping(ResultSet resultSet){
        List<User> list = new ArrayList<>();
        //遍历添加进数组
        try {
            while(resultSet.next()){
                //获取具体参数
                Long id = resultSet.getLong("id");
                String name = resultSet.getString("name");
                String password = resultSet.getString("password");
                String code = resultSet.getString("code");
                //实例化对象
                User user = new User();
                user.setPassword(password);
                user.setName(name);
                user.setId(id);
                user.setCode(code);
                list.add(user);
            }

        }catch (SQLException e){
            log.error(e.getMessage(),e);
        }

        return list;
    }
}
